﻿namespace PowerSong.UserInterface
    {
    using System;
    using System.Collections.Generic;
    using System.Windows;
    using PowerSong.BibleSupport;
    using PowerSong.SongDatabase;

    /// <summary>
    /// Interaction logic for SelectBibleChaptersWindow.xaml
    /// </summary>
    public partial class SelectBibleChaptersWindow : Window
        {

        private BibleIndex activeBible = null;

        public Song[] SelectedChapters { get; private set; }

        private void SetSelectedChapters()
            {
            var result = new List<Song>();
            if (activeBible != null)
                {
                foreach (string chapterName in ChaptersListBox.GetCheckedItems())
                    {
                    var chapterText = activeBible.FindChapter(chapterName);
                    var verses = chapterText.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);
                    var song = new Song(chapterName, verses);
                    song.Copyright = activeBible.Translation;
                    result.Add(song);
                    }
                }
            this.SelectedChapters = result.ToArray();
            }

        public SelectBibleChaptersWindow()
            {

            InitializeComponent();

            // Show available bible translations
            foreach (var translation in BibleTranslationManager.GetTranslations())
                {
                TranslationsComboBox.Items.Add(translation);
                }

            // Select currently active bible translation
            if (State.Settings.ActiveBibleTranslation != null)
                {
                TranslationsComboBox.SelectedItem = State.Settings.ActiveBibleTranslation;
                }

            }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
            {

            // Close bible translation if one is currently open
            if (activeBible != null) activeBible.Close();

            // Close the window
            this.DialogResult = false;

            }

        private void OKButton_Click(object sender, RoutedEventArgs e)
            {

            // Set the selected chapters
            SetSelectedChapters();

            // Close bible translation if one is currently open
            if (activeBible != null) activeBible.Close();

            // Close the window
            this.DialogResult = true;

            }

        private void TranslationsComboBox_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
            {

            // Close bible if another is currently open
            if (activeBible != null)
                {
                activeBible.Close();
                }

            // Get the translation that was selected and load it
            var selectedTranslation = (string)TranslationsComboBox.SelectedItem;
            activeBible = BibleTranslationManager.LoadTranslation(selectedTranslation);

            // Show the chapters definined in the selected bible
            ChaptersListBox.Items.Clear();
            foreach (var chapter in activeBible.GetAllChapters())
                {
                ChaptersListBox.Items.Add(new CheckListBoxItem(chapter));
                }

            }

        }

    }
